
// Tables-DataTables.js
// ====================================================================
// This file should not be included in your project.
// This is just a sample how to initialize plugins or components.
//
// - ThemeOn.net -



$(window).on('load', function() {
    // DATA TABLES
    // =================================================================
    // Require Data Tables
    // -----------------------------------------------------------------
    // http://www.datatables.net/
    // =================================================================

    $.fn.DataTable.ext.pager.numbers_length = 5;
    // Row selection (single row)
    // -----------------------------------------------------------------
    var rowSelection = $('#demo-dt-selection').DataTable({
        "processing": true,
        serverSide: true,
        //lengthChange:true,
        paging: true,
        //"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        pageLength: 20,
        ajax: {
            url: "/admin/customer/list",
            type: 'post',
            dataSrc: 'data'
        },
        columns: [
            {data: 'id'},
            {data: 'name'},
            {data: 'company'},
            {data: 'phone'},
            {data: 'url'},
            {data: 'createTime'}
        ],
        "responsive": true,
        "language": {
            "paginate": {
              "previous": '<i class="demo-psi-arrow-left"></i>',
              "next": '<i class="demo-psi-arrow-right"></i>'
            }
        },
        "dom": '<"toolbar">frtip',
        "rowCallback": function (row, data, index) {
            $('td:eq(0)', row).html('<a target="_blank" href="/admin/customer/edit/' + data["id"] + '">' + data["id"] + '</a>');
            $('td:eq(4)', row).html('<a target="_blank" href="' + data["url"] + '">' + data["url"] + '</a>');
            $('td:eq(5)', row).html(timeStamp2String(data["createTime"]));
            // if ( data[4] == "A" ) {
            // }
        }
    });

    $('#demo-dt-selection').on( 'click', 'tr', function () {
        if ( $(this).hasClass('selected') ) {
            $(this).removeClass('selected');
        }
        else {
            rowSelection.$('tr.selected').removeClass('selected');
            $(this).addClass('selected');
        }
    } );

    $('#customer-custom-toolbar').appendTo($("div.toolbar"));
    $('#customer-custom-toolbar-add').appendTo($("div.toolbar"));

    $('#demo-dt-delete-btn').click( function () {
        rowSelection.row('.selected').remove().draw(false);
    } );

    $('#customer-dt-add-btn').click(function () {
        window.location.href = "/admin/customer/add";
    } );

    
});
